/* eslint-disable indent */
/* eslint-disable @typescript-eslint/indent */
import React, { useEffect, Fragment } from 'react';
import { connect } from 'umi';
import { Form, Divider, Button, Icon, message, ConfigProvider } from 'antd';
import UploadOss from '@/components/UploadOss';
import { BottomAction } from '@/components';

import Form1 from '@/pages/User/Confidential/components/Form1';
import Form2 from '@/pages/User/Confidential/components/Form2';
import Form3 from '@/pages/User/Confidential/components/Form3';
import Form4 from '@/pages/User/Confidential/components/Form4';
import Form5 from '@/pages/User/Confidential/components/Form5';
import Form6 from '@/pages/User/Confidential/components/Form6';
import Form7 from '@/pages/User/Confidential/components/Form7';
import styles from '../TalentPoolList.less';

const TalentPoolForm = ({
    dispatch,
    form,
    isEdit,
    candidate,
    onReturn,
    showFileList,
    uploadResource,
    onSetFileResource,
    submitLoading,
    dictionary,
    required,
    user
}) => {
    const { currentUser = {} } = user;
    const { getFieldDecorator } = form;
    const { companyId: userCompanyId } = currentUser;
    // 公司判断表单渲染是新加坡还是厦门  如果是首次创建简历 公司为登录者的公司id 如果编辑简历 公司为简历者自身的公司id
    const { companyId = userCompanyId, ...others } = candidate;
    const initialFormInfo = { ...others, companyId };
    let subFormProps;
    if (!isEdit) {
        subFormProps = { form, dict: dictionary, initialFormInfo, disabled: true, required };
    } else {
        subFormProps = { form, dict: dictionary, initialFormInfo, required };
    }

    // const asian = talentPool.asianList; // 籍贯

    /**
     * showFileList 需要设置的文件数据，比如说用来展示目前有多少条上传的附件
     * uploadResource    暂时为null，主要作用返回后端用的
     * showFileListReturn   文件信息
     * uploadResourceReturn     需要传给后端的数据
     */
    const changeAllFileList = (showFileListReturn, uploadResourceReturn) => {
        if (uploadResourceReturn.keyNameAndOpt.length <= 10 && showFileListReturn.length <= 10) {
            onSetFileResource(showFileListReturn, uploadResourceReturn);
        } else {
            message.error('上传数量超出10条');
        }
    };

    // 获取简历信息数据字典
    const getDict = () => {
        dispatch({
            type: 'dictionary/fetchDictionaryList',
            payload: {
                codes: [
                    'agreeType',
                    'sexType',
                    'healthyState',
                    'emergencyRelationship',
                    'household',
                    'liveTogether',
                    'backCheckAccepted',
                    'SingaporeRecruitWay',
                    'academicYear',
                    'industryType',
                    'ChinaRecruitWay'
                ]
            }
        });
    };
    useEffect(() => {
        getDict();
    }, []);

    const dividerStyle = { marginTop: 8 };
    return (
        <div className={styles.formWrap}>
            <ConfigProvider getPopupContainer={trigger => trigger.parentNode}>
                {/* <FormContainer> */}
                <Form1 {...subFormProps} />
                <Form.Item label="附件" vvspan={24} labelCol={{ md: 4 }} wrapperCol={{ md: 20 }}>
                    {getFieldDecorator(
                        'resourceRelation',
                        {}
                    )(
                        <Fragment>
                            <UploadOss
                                disabled={!isEdit}
                                uploadUrl="/api/resource/public/oss/producePutPreSignUrls"
                                checkFileSize={10}
                                checkFileType="png、jpg、doc、docx、pdf、xlsx、xls"
                                categoryCode="talentFile"
                                sysCode="OA"
                                fileList={showFileList}
                                uploadResource={uploadResource}
                                setFileList={changeAllFileList}
                                checkFileTypeMsg="只能上传png、jpg、doc、docx、pdf、xlsx、xls格式的文件！"
                                checkFileSizeMsg="文件大小不能超过10MB！"
                            >
                                <Button type="primary" ghost disabled={!isEdit}>
                                    <Icon type="upload" />
                                    上传文件
                                </Button>
                            </UploadOss>
                            <div>支持png、jpg、doc、docx、pdf、xlsx、xls上传，大小为10M以内的文件。</div>
                        </Fragment>
                    )}
                </Form.Item>
                <Divider style={dividerStyle} />
                <Form2 {...subFormProps} />
                <Divider style={dividerStyle} />
                <Form3 {...subFormProps} />
                <Divider style={dividerStyle} />
                <Form4 {...subFormProps} />
                <Divider style={dividerStyle} />
                <Form5 {...subFormProps} />
                <Divider style={dividerStyle} />
                <Form6 {...subFormProps} />
                <Divider style={dividerStyle} />
                <Form7 {...subFormProps} />
                <Divider style={dividerStyle} />
                {isEdit ? (
                    <BottomAction>
                        <Button type="primary" htmlType="submit" loading={submitLoading}>
                            提交
                        </Button>
                        <Button
                            style={{ marginLeft: 15 }}
                            onClick={() => {
                                onReturn();
                            }}
                        >
                            取消
                        </Button>
                    </BottomAction>
                ) : (
                    ''
                )}
                {/* </FormContainer> */}
            </ConfigProvider>
        </div>
    );
};
export default connect(({ loading, user, talentPool, dictionary }) => ({
    loading,
    user,
    talentPool,
    dictionary
}))(Form.create()(TalentPoolForm));
